//  https://www.luogu.com.cn/problem/P1048

#include<iostream>
using namespace std;

const int N = 1010;
int t[N], v[N];
int n, m;
int dp[N][N];

int main()
{
    cin >> m >> n;
    for (int i = 1;i <= n;i++) cin >> t[i] >> v[i];

    for (int i = 1;i <= n;i++)
    {
        for (int j = 0;j <= m;j++)
        {
            dp[i][j] = dp[i - 1][j];
            if (j >= t[i])
                dp[i][j] = max(dp[i][j], dp[i - 1][j - t[i]] + v[i]);
        }
    }
    cout << dp[n][m] << endl;
    return 0;
}